//
//  BBTrackEventManager.m
//  yingStep
//
//  Created by 你好 on 2020/7/16.
//  Copyright © 2020 花生短视频. All rights reserved.
//审核、点击、发点击消息到钉钉

#import "BBTrackEventManager.h"
//#import <RSSwizzle/RSSwizzle.h>

@interface BBTrackEventManager()

@property(nonatomic, copy) NSString *bundleShortVersionString;
@property(nonatomic, copy) NSString *bundleVersion;

+ (instancetype)sharedInstance;
@end

@implementation BBTrackEventManager

+ (instancetype)sharedInstance {
    static BBTrackEventManager *_sharedInstance = nil;

    if (_sharedInstance == nil) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _sharedInstance = [[[self class] alloc] init];
        });
    }

    return _sharedInstance;
}

- (instancetype)init {
    if (self = [super init]) {
        
    }
    return self;
}

+ (void)reportText:(NSString *)text {
    DebugLog(@"全局监控触摸事件，操作了：%@",text);
//    [DWBToast showCenterWithText:[NSString stringWithFormat:@"点击了：%@",text]];
    
}

- (NSString *)bundleShortVersionString {
    if (!_bundleShortVersionString) {
        _bundleShortVersionString = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    }
    return _bundleShortVersionString;
}

- (NSString *)bundleVersion {
    if (!_bundleVersion) {
        _bundleVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"];
    }
    return _bundleVersion;
}
@end

@implementation UIViewController (AlplpeRievew)

+ (void)load{
//    [RSSwizzle swizzleInstanceMethod:@selector(viewDidAppear:) inClass:[UIViewController class] newImpFactory:^id(RSSwizzleInfo *swizzleInfo) {
//        return ^void(__unsafe_unretained id self, BOOL animated) {
//            NSLog(@"用户操作了");
//            if ([[self class] isKindOfClass:[UINavigationController class]]) {
//                return;
//            }
//            else if ([NSStringFromClass([self class]) hasPrefix:@"UI"]) {
//                return;
//            }
//            else if ([NSStringFromClass([self class]) hasPrefix:@"_UI"]) {
//                return;
//            }
//            else if ([[self class] isSubclassOfClass:[UIViewController class]]) {
//                NSString *text = [NSString stringWithFormat:@"e:%@", [self class]];
//                [BBTrackEventManager reportText:text];
//            }
//
//        };
//    } mode:RSSwizzleModeAlways key:NULL];
//
//    [RSSwizzle swizzleInstanceMethod:@selector(viewDidDisappear:) inClass:[UIViewController class] newImpFactory:^id(RSSwizzleInfo *swizzleInfo) {
//        return ^void(__unsafe_unretained id self, BOOL animated) {
//            NSLog(@"用户操作了2");
//            if ([[self class] isKindOfClass:[UINavigationController class]]) {
//                return;
//            }
//            else if ([NSStringFromClass([self class]) hasPrefix:@"UI"]) {
//                return;
//            }
//            else if ([NSStringFromClass([self class]) hasPrefix:@"_UI"]) {
//                return;
//            }
//            else if ([[self class] isSubclassOfClass:[UIViewController class]]) {
//                NSString *text = [NSString stringWithFormat:@"l:%@", [self class]];
//                [BBTrackEventManager reportText:text];
//            }
//        };
//    } mode:RSSwizzleModeAlways key:NULL];
//
//    [RSSwizzle swizzleInstanceMethod:@selector(sendAction:to:from:forEvent:) inClass:[UIApplication class] newImpFactory:^id(RSSwizzleInfo *swizzleInfo) {
//        return ^BOOL(__unsafe_unretained id self, SEL action, id target, id sender, UIEvent *event) {
//            NSLog(@"用户操作3");
//            BOOL (*originalIMP)(__unsafe_unretained id, SEL, SEL, id, id, UIEvent*);
//            originalIMP = (__typeof(originalIMP))[swizzleInfo getOriginalImplementation];
//            BOOL res = originalIMP(self, @selector(sendAction:to:from:forEvent:), action, target, sender, event);
//
//            __block BOOL isTouchEnd = NO;
//
//            if (event && event.allTouches.count > 0) {
//                [event.allTouches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
//                    if (obj.phase == UITouchPhaseEnded) {
//                        isTouchEnd = YES;
//                    }
//                }];
//            }
//
//            if (isTouchEnd) {
//                if ([sender isKindOfClass:[UIButton class]]) {
//                    [BBTrackEventManager reportText:[NSString stringWithFormat:@"c:%@", [sender currentTitle] ?  : @"其他"]];
//                }
//            }
//
//            return res;
//        };
//    } mode:RSSwizzleModeAlways key:NULL];
//
//    Class AppDelegate = NSClassFromString(@"AppDelegate");
//    if (AppDelegate) {
//        RSSwizzleInstanceMethod(AppDelegate,
//                                @selector(applicationDidBecomeActive:),
//                                RSSWReturnType(void),
//                                RSSWArguments(UIApplication *application),
//                                RSSWReplacement({
//            RSSWCallOriginal(application);
//            [BBTrackEventManager reportText:@"B-A"];
//        }), RSSwizzleModeAlways, NULL);
//        RSSwizzleInstanceMethod(AppDelegate,
//                                @selector(applicationDidEnterBackground:),
//                                RSSWReturnType(void),
//                                RSSWArguments(UIApplication *application),
//                                RSSWReplacement({
//            RSSWCallOriginal(application);
//            [BBTrackEventManager reportText:@"E-B"];
//        }), RSSwizzleModeAlways, NULL);
//    }
}

@end
